package test.baojia.act;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import test.baojia.testcase.TestCommon;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

public class JsonScriptRunner {

    public static void runScript(
            JSONObject testJson ,
            WebDriver driver ,
            Properties p ,
            boolean doCatch ,
            JSONObject parentTestJson ) throws  Exception {

        Logger logger = LoggerFactory.getLogger(JsonScriptRunner.class);
        String testName = null;
        try{
            JSONArray acts = testJson.getJSONArray("acts");
            testName = testJson.getString("testName");

            for(int i = 0 ; i < acts.size() ; i++){
                JSONObject actJo = acts.getJSONObject(i);
                logger.info("{} actJo : {}" ,testName , actJo.toString());
                Boolean valid = actJo.getBoolean("valid");
                if(valid == null){
                    valid = true;
                }

                if(valid){
                    final String actName = actJo.getString("actName");
                    final String actType = actJo.getString("actType");
                    final Act act = AbstractAct.loadAct(actName , actType);
                    final JSONObject context = actJo.getJSONObject("context");
                    if(!parentTestJson.isEmpty()){
                        context.put("parentContext" , parentTestJson);
                    }
                    final boolean debugger = actJo.containsKey("debugger") ? actJo.getBoolean("debugger") : false;
                    if(debugger){
                        logger.info("{} debugger : {}" , testName , i);
                    }
                    act.run(context , driver , p, parentTestJson);
                }
            }
        }catch(Error e){
            if(doCatch){
                e.printStackTrace();
                String screenShotFile = taskScreenShot(driver , testName , p.getProperty("screenshot.filepath"));
                logger.info("Test {} 's error screen shot file : {} ." , TestCommon.getTestID(p) , screenShotFile);
            }
            throw e;
        }
    }

    public static String taskScreenShot(WebDriver driver , String testName , String filePath){
        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

        final String fileName = testName + "_" + new Date().getTime() + ".png";
        final String fullPath = filePath + fileName;

        // 需要指定图片的保存路径及文件名
        try {
            FileUtils.copyFile(srcFile, new File(fullPath));
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            return fullPath;
        }
    }

}
